package service

import (
	"server/global"
	"server/plugin/mall/model"
	"server/plugin/mall/model/request"
)

var A商品sku = new(shangpinsku)

type shangpinsku struct{}

// CreateA商品sku 创建商品sku记录
// Author [yourname](https://github.com/yourname)
func (s *shangpinsku) CreateA商品sku(shangpinsku *model.A商品sku) (err error) {
	err = global.GVA_DB.Create(shangpinsku).Error
	return err
}

// DeleteA商品sku 删除商品sku记录
// Author [yourname](https://github.com/yourname)
func (s *shangpinsku) DeleteA商品sku(ID string) (err error) {
	err = global.GVA_DB.Delete(&model.A商品sku{}, "id = ?", ID).Error
	return err
}

// DeleteA商品skuByIds 批量删除商品sku记录
// Author [yourname](https://github.com/yourname)
func (s *shangpinsku) DeleteA商品skuByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]model.A商品sku{}, "id in ?", IDs).Error
	return err
}

// UpdateA商品sku 更新商品sku记录
// Author [yourname](https://github.com/yourname)
func (s *shangpinsku) UpdateA商品sku(shangpinsku model.A商品sku) (err error) {
	err = global.GVA_DB.Model(&model.A商品sku{}).Where("id = ?", shangpinsku.ID).Updates(&shangpinsku).Error
	return err
}

// GetA商品sku 根据ID获取商品sku记录
// Author [yourname](https://github.com/yourname)
func (s *shangpinsku) GetA商品sku(ID string) (shangpinsku model.A商品sku, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&shangpinsku).Error
	return
}

// GetA商品skuInfoList 分页获取商品sku记录
// Author [yourname](https://github.com/yourname)
func (s *shangpinsku) GetA商品skuInfoList(info request.A商品skuSearch) (list []model.A商品sku, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.A商品sku{})
	var shangpinskus []model.A商品sku
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Find(&shangpinskus).Error
	return shangpinskus, total, err
}

func (s *shangpinsku) GetA商品skuPublic() {

}
